package com.basic.controller;

import com.basic.util.TokenUtil;
import com.code.domain.AjaxResult;
import com.code.domain.CommonResult;
import com.code.domain.GpmartUtil;
import com.code.domain.PageObject;
import com.code.entity.User;
import com.code.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@ApiOperation(value = "用户模块",notes = "用户模块")
@Controller
@RequestMapping(value = "/basic/user")
@Slf4j
@CrossOrigin
public class UserBasicController {

    @Autowired
    private IUserService userService;

    /**
     * 通过该方法对用户列表进行查询
     * @param pageObject
     * @param user
     * @param request
     * @param response
     * @return
     */
    @GetMapping(value = "/queryBasicUserLayUiPage")
    @ApiOperation(value = "前端框架分页查询",notes = "前端框架分页查询")
    public @ResponseBody CommonResult queryUserLayUiPage(PageObject pageObject, User user, HttpServletRequest request, HttpServletResponse response){

        List<User> countList = userService.queryUserList(user);
        List<User> list = userService.findUserPage(GpmartUtil.PageBefore(pageObject.getPage(),pageObject.getLimit()),pageObject.getLimit(),user);
        return new CommonResult(0,"操作成功",countList.size(),list);
    }

    /**
     * 通过该方法进行用户的新增操作
     * @param user
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "新增用户",notes = "新增用户")
    @PostMapping("/addUser")
    public @ResponseBody AjaxResult addUserLayUi(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int result  =  userService.addUserLayUi(user);
        if(result == 1){
           return AjaxResult.success();
        }else{
            throw new RuntimeException("新增用户失败");
        }
    }

    @ApiOperation(value = "删除用户",notes="删除用户")
    @DeleteMapping("/deleteUserLayUi/{userid}")
    public @ResponseBody AjaxResult deleteUserLayUi(@PathVariable("userid") String userid, HttpServletRequest request, HttpServletResponse response){
    //进行删除操作
        int result = userService.deleteUserByuserid(userid);
        if(result == 1){
            return AjaxResult.success();
        }else{
            throw new RuntimeException("删除用户失败");
        }

    }

    @ApiOperation(value = "删除用户信息",notes = "删除用户信息")
    @GetMapping(value = "/deleteUserReact")
    public @ResponseBody AjaxResult deleteUserReact( @RequestParam(value="userid")String userid , HttpServletRequest request,HttpServletResponse response){
        //进行删除操作
        int result = userService.deleteUserByuserid(userid);
        if(result == 1){
            return AjaxResult.success();
        }else{
            throw new RuntimeException("删除用户失败");
        }
    }


    @ApiOperation(value = "根据主键进行查询",notes = "根据主键进行查询")
    @GetMapping("/queryUserByUserId/{userid}")
    public @ResponseBody AjaxResult queryUserByUserId(@PathVariable("userid") String userid, HttpServletRequest request, HttpServletResponse response){
        //根据逐渐查询
        User user = userService.queryUserByUserId(userid);
        return  AjaxResult.success("查询成功",user);
    }

    /**
     * /basic/user/updateUser
     * @param user
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "修改用户信息",notes = "修改用户信息")
    @PostMapping("/updateUser")
    public @ResponseBody AjaxResult updateUser(User user, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        int flag = userService.updateUser(user);
        if(flag==1){
            return AjaxResult.success();
        }else{
            throw new RuntimeException("修改用户失败");
        }
    }

    /**
     * /basic/user/queryBasicUserAll
     * @return
     */
    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户信息")
    @GetMapping("/queryBasicUserAll")
    public @ResponseBody CommonResult queryBasicUserAll(User user,HttpServletRequest request,HttpServletResponse response){
        List<User> list = userService.queryBasicUserAll();
        //获取用户信息
        String id = TokenUtil.getMemberIdByJwtToken(request);
        return new CommonResult(0,"操作成功",list.size(),list);
    }

    /**
     * /basic/user/getRefreshUser
     * @param user
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "刷新用户信息" ,notes = "刷新用户信息")
    @PostMapping("/getRefreshUser")
    public @ResponseBody AjaxResult getRefreshUser(User user,HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> flag =  userService.getRefreshUser();
            return AjaxResult.success(flag);
    }
}